//package com.example.itrain.demo.filters;
//
//import com.example.itrain.demo.utils.JwtUtils;
//import io.jsonwebtoken.Claims;
//import jakarta.servlet.FilterChain;
//import jakarta.servlet.ServletException;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import java.io.IOException;
//import java.util.Collections;
//
//@Component
//public class JwtAuthenticationFilter extends OncePerRequestFilter {
//
//    private final JwtUtils jwtUtils;
//
//    public JwtAuthenticationFilter(JwtUtils jwtUtils) {
//        this.jwtUtils = jwtUtils;
//    }
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
//            throws ServletException, IOException {
//
//        String authHeader = request.getHeader("Authorization");
//
//        if (authHeader != null && authHeader.startsWith("Bearer ")) {
//            String token = authHeader.substring(7);
//            if (!jwtUtils.isTokenExpired(token)) {
//                Claims claims = jwtUtils.parseToken(token);
//                String username = claims.getSubject();
//
//                // 创建一个User对象和权限信息
//                User user = new User(username, "", Collections.emptyList());
//
//                // 将用户信息存入SecurityContext
//                SecurityContextHolder.getContext().setAuthentication(
//                        new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities())
//                );
//            }
//        }
//        chain.doFilter(request, response);
//    }
//}
